package com.zl.tby.screen.enums;

import com.zl.tby.core.base.util.ToolsUtil;
import com.zl.tby.dto.TbyEcologyInfoQuery;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author changpeng
 * @version 1.0
 * @since 2024/11/6 21:25
 */
public enum TbyEcologyXAxisEnum {
    HOUR("按照小时统计") {
        @Override
        public List<String> getXAxisList(TbyEcologyInfoQuery query) {
            List<String> xAxisList = new ArrayList<>();
            for (int i = 0; i < 24; i++) {
                if (i == 0 || i % 3 == 0) {
                    xAxisList.add(i < 10 ? "0" + i : i + "");
                }
            }
            return xAxisList;
        }

        @Override
        public String getXAxisValue(String value) {
            return value + ":00";
        }

        @Override
        public String getGroupByColumn() {
            return "";
        }
    },
    DAY("按照日统计") {
        @Override
        public List<String> getXAxisList(TbyEcologyInfoQuery query) {
            List<String> xAxisList = new ArrayList<>();
            long startTime = query.getStartTime();
            while (true) {
                xAxisList.add(ToolsUtil.dateUtil2String(new Date(startTime), "yyyy-MM-dd"));
                startTime = startTime + (1000 * 60 * 60 * 24);
                if (startTime > query.getEndTime()) {
                    break;
                }
            }
            return xAxisList;
        }

        @Override
        public String getXAxisValue(String value) {
            String[] array = ToolsUtil.splitStr(value, '-');
            return array[1] + "/" + array[2];
        }

        @Override
        public String getGroupByColumn() {
            return "DATE_FORMAT(report_date, '%Y-%m-%d')";
        }
    },
    MONTH("按照月统计") {
        @Override
        public List<String> getXAxisList(TbyEcologyInfoQuery query) {
            List<String> xAxisList = new ArrayList<>();
            long startTime = query.getStartTime();
            while (true) {
                xAxisList.add(ToolsUtil.dateUtil2String(new Date(startTime), "yyyy-MM"));
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(new Date(startTime));
                calendar.add(Calendar.MONTH, 1);
                calendar.set(Calendar.DAY_OF_MONTH, 1);
                startTime = calendar.getTimeInMillis();
                if (startTime > query.getEndTime()) {
                    break;
                }
            }
            return xAxisList;
        }

        @Override
        public String getXAxisValue(String value) {
            String[] array = ToolsUtil.splitStr(value, '-');
            return array[0].substring(2, 4) + "/" + array[1];
        }

        @Override
        public String getGroupByColumn() {
            return "DATE_FORMAT(report_date, '%Y-%m')";
        }
    },
    YEAR("按照年统计") {
        @Override
        public List<String> getXAxisList(TbyEcologyInfoQuery query) {
            List<String> xAxisList = new ArrayList<>();
            int start = ToolsUtil.toLong(ToolsUtil.splitStr(query.getStartDate(), '-')[0], 0L).intValue();
            int end = ToolsUtil.toLong(ToolsUtil.splitStr(query.getEndDate(), '-')[0], 0L).intValue();
            for (int i = start; i <= end; i++) {
                xAxisList.add(i + "");
            }
            return xAxisList;
        }

        @Override
        public String getXAxisValue(String value) {
            return value;
        }

        @Override
        public String getGroupByColumn() {
            return "DATE_FORMAT(report_date, '%Y')";
        }
    };
    private String value;
    TbyEcologyXAxisEnum(String value) {
        this.value = value;
    }

    public static TbyEcologyXAxisEnum getByCode(String code) {
        TbyEcologyXAxisEnum[] array = values();
        for (TbyEcologyXAxisEnum tbyEcologyXAxisEnum : array) {
            if (tbyEcologyXAxisEnum.toString().equals(code)) {
                return tbyEcologyXAxisEnum;
            }
        }
        return null;
    }

    public abstract List<String> getXAxisList(TbyEcologyInfoQuery query);
    public abstract String getXAxisValue(String value);
    public abstract String getGroupByColumn();

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}
